import { Component, OnInit } from '@angular/core';
import { MessageService } from 'src/app/core/service';
import { UploadFileServer } from 'src/app/core/service/uploadFile.service';
import { ApiService, urlService } from 'src/app/core/api';
import { Router } from '@angular/router';

@Component({
  selector: 'my-edit-img',
  templateUrl: './edit-img.component.html',
  styleUrls: ['./edit-img.scss']
})
export class EditImgComponent implements OnInit {

  url = 'account';

  loading = false;
  isShowFImg: boolean = true;
  
  ngOnInit() {
    this.msg_1.sendMessage(this.url);
    this.loaPage();
  };
  /**
   * test img-cropper
   * 
   */
  imageChangedEvent: any = '';
  croppedImage: any = '';
  selFile:any;

  fileChangeEvent(event: any): void {
    if(event.target.files.length > 0){
      this.imageChangedEvent = event;
      this.isShowFImg=false;
    };
  };
  imageCropped(image: any) {
    this.croppedImage = image;
  };
  
  imageLoaded() {
      // show cropper
  };
  loadImageFailed() {
      // show message
  };
/** end------------------------------ */
   /* 更新图片 */
  bigurl:any;
  public uploadImage(e) {
    
    
    const image = this.imageChangedEvent.target.files[0];
    // const image = e.target.files[0];
    
    this._upload.uploadImage('/user/updateImageForWeb.do', image, this.croppedImage.base64)
      .then(response => {
          this._imgURL = response['data'].image;
      })
      .then(() =>  this.router.navigateByUrl("/act/myacy/account"))
      .catch(err => console.log(err));

  };

  async loaPage() {
    let data = await this._getAJX('个人中心[Web前端专用]',{});
    this.api.getData(data,false) ? this._successListOk(data.data) : '';
  };
  private _successListOk(data):void{
    this._imgURL = data.image;
  };
  private  _getAJX(url:string, data:any):any{//接口交換
    let success = this.api.post(urlService('dev', url), data).toPromise();
    return success;
  };

  private _imgURL:string;
  constructor(
    private api: ApiService,
    private msg_1: MessageService,
    private _upload: UploadFileServer,
    private router: Router
  ) { };
}
